package com.huang.day1;


import java.util.concurrent.locks.ReentrantLock;

/**
 * @author HuangShen
 * @description  多线程 企业级模板
 * @create 2021-03-21 10:06
 */
public class SaleTicket {

    public static void main(String[] args) {
        Ticket ticket = new Ticket();

        for (int j = 0; j < 10; j++) {
            new Thread(() -> {
                for (int i = 0; i < 40; i++) {
                    ticket.saleTicket();
                }
            }, j + "").start();
        }


    }


}


/**
 * 线程操纵的  资源类
 */
class Ticket {
    private int number = 30;
    private ReentrantLock lock=new ReentrantLock();

    public  void saleTicket() {

        lock.lock();
        if (number > 0) {
            System.out.println(Thread.currentThread().getName() + ":卖出第" + (number--) + "张票，还剩下：" + number + "张票");
        }
        lock.unlock();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}